<?php
/**
 * 角色管理
 *
 * PHP Version 5.6
 *
 * @author    guanxu <510104561@qq.com>
 * @time      2017/08/11
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Common\ConstantUtil;

Class RoleController extends CommonController {

    /**
     * 角色列表页面
     */
    public function index() {
        $list = M("role")->where(["status"=>ConstantUtil::GLOBAL_TRUE])->select();
        $this->assign("list",$list);
        $this->display('role/index');
    }

    /**
     * 角色添加
     */
    public function add() {
        $name = I("post.name");
        $data["name"] = $name;
        $data["status"] = ConstantUtil::GLOBAL_TRUE;
        $data["create_time"] = date("Y-m-d H:i:s");
        $result = M("role")->add($data);
        if(empty($result)) {
            $this->success('添加失败!');
        } else {
            $this->success('添加成功!');
        }
    }

    /**
     * 角色添加页面
     */
    public function addView() {
        $this->display("role/add");
    }

    public function editorView() {
        $id = I("get.id");
        $list = M("role")->where(["id"=>$id])->find();
        $this->assign(["list"=>$list,"id"=>$id]);
        $this->display("role/editor");
    }

    public function editor(){
        $id = I("post.id");
        $name = I("post.name");
        $result = M("role")->where(["id"=>$id])->save(["name"=>$name]);
        if(empty($result)) {
            $this->success('添加失败!');
        } else {
            $this->success('添加成功!');
        }
    }

    public function delete(){
        $id = I("get.id");
        $result = D("role")->deleteRole($id);
        if(empty($result)) {
            $this->success('添加失败!');
        } else {
            $this->success('添加成功!');
        }

    }

    /**
     * 角色关系编辑显示
     * @return string
     */
    public function relationEditorView() {

        $id = I("get.id");
        if(empty($id)) return '';
        $select = M("role_relation")->where(["role_id"=>$id])->getField("modules_func_id",true);
        $modules =  M("role_relation")->join("modules_func on modules_func.id = role_relation.modules_func_id")->where(["role_relation.role_id"=>$id])->field("modules_func.modules_id")->distinct(true)->getField("modules_id",true);
        $list = D("classify")->getClassifyModules();
        $this->assign(["list"=>$list,"id"=>$id,"select"=>$select,"modules"=>$modules]);
        $this->display("role/relation_editor");
    }


    /**
     * 角色关系编辑
     */
    public function relationEditor() {

        $modules = I("");
        $result = D("RoleRelation")->add($modules);
        if(empty($result)) {
            $this->error('添加失败!');
        } else {
            $this->success('添加成功!');
        }
    }


    /**
     * 角色关联用户编辑显示
     * @return string
     */
    public function userEditorView() {

        $id = I("get.id");
        if(empty($id)) return '';
        $list = M("user")->where(["rank"=>ConstantUtil::POWER_MANAGE,"status"=>ConstantUtil::GLOBAL_TRUE])->select();
        //$role = M("role_user")->where(["role_id"=>$id])->field("user_id")->select();
        $role = M("role_user")->where(["role_id"=>$id])->getField("user_id",true);
        $this->assign(["list"=>$list,"id"=>$id,"role"=>$role]);
        $this->display("role/user_editor");

    }

    /**
     * 角色关联用户编辑
     */
    public function userEditor() {

        $role_id    = I("post.id");
        $user = I("post.user");
        M("role_user")->where(["role_id"=>$role_id])->delete();
        if(empty($user)) goto End;
        $date = date("Y-m-d H:i:s");
        try {
            foreach($user as $val) {
                $data = [];
                $data["user_id"] = $val;
                $data["role_id"] = $role_id;
                $data["create_time"] = $date;
                M("role_user")->add($data);
            }
        } catch ( \PDOException $e) {
            $this->error('添加失败!');
        }
        End:
        $this->success('添加成功!');
    }




}